$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/buserwithdrawlog/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 30, key: true },
			{ label: '用户ID', name: 'userId', index: 'user_id',align: "center", width: 50 },
			{ label: '用户昵称', name: 'userName', index: 'userName',align: "center", width: 50 },
			{ label: '提现金额', name: 'amount', index: 'amount',align: "center", width: 50 },
            { label: '申请时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			{ label: '账号名', name: 'fullName', index: 'full_name',align: "center", width: 50 },
			{ label: '二维码', name: 'qrcodeUrl', index: 'qrcode_url', width: 80,align: "center", sortable: false, editable: false, formatter: function (value,options,rowData) {
					let userId = rowData["userId"];
					if(value==null || value==""){
						return "";
					}else {
						return "<img src='" + value + "' height='60' onclick='vm.queryQrcode(this,"+userId+")' />";
					}
				}
			},
			{ label: '银行名称', name: 'bankName', index: 'bank_name',align: "center", width: 80 },
            { label: '支行名称', name: 'subBranch', index: 'sub_branch',align: "center", width: 100 },
            { label: '银行账号', name: 'bankAccount', index: 'bank_account',align: "center", width: 120 },
            { label: '电话', name: 'phone', index: 'phone',align: "center", width: 70 },
            { label: '公众号', name: 'gzhName', index: 'gzhName',align: "center", width: 60 },
            // { label: '状态', name: 'processStatus', index: 'process_status',align: "center", width: 80,
            //     formatter:function(value,options,rowData){
            //         if(value==0){
            //             return "未处理";
            //         }else if(value==1){
            //             return "已打款";
            //         }else if(value==2){
            //             return "已拒绝";
            //         }
            //     }
            // },
			{ label: '操作', name: 'processStatus', index: 'process_status',align: "center", width: 80,
				formatter:function(value,options,rowData){
					if(value==0){
						var userId=rowData["userId"];
						var amount=rowData["amount"];
						var id=rowData["id"];
						return "<a class='btn btn-primary' onclick=vm.dakuan("+userId+","+amount+","+id+")>"+'处理打款'+"</a>";
					}else if(value==1){
						return "<a class='btn btn-primary' disabled='disabled'>"+'已打款'+"</a>";
					}else if(value==2){
						return "<a class='btn btn-primary' disabled='disabled'>"+'已拒绝'+"</a>";
					}
				}

			},
        ],
		viewrecords: true,
        height: "100%",
        rowNum: 12,
		rowList : [12,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bUserWithdrawLog: {}
	},
	methods: {
		query: function () {
			//vm.reload();
            vm.showList = true;
            var processStatus=$("#processStatus").val();
            var userId=$("#userId").val();
            var userName=$("#userName").val();
            $("#jqGrid").jqGrid('setGridParam',{
                page:1,
                postData:{"userId":userId,"userName":userName,"processStatus":processStatus}
            }).trigger("reloadGrid");
		},
		dakuan: function(u,m,e){
			var myform = new FormData();
			myform.append('userId', u);
			myform.append('amount', m);
			myform.append('id', e);
			$.ajax({
				type: "POST",
				url: baseURL + "sys/buserwithdrawlog/remit",
				async: false,
				contentType: false,
				processData: false,
				data: myform,
				success: function (r) {
					if(r.code === 0){
						alert("打款成功");
						vm.reload();
					}else{
						alert(r.msg);
					}
				}
			});
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bUserWithdrawLog = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bUserWithdrawLog.id == null ? "sys/buserwithdrawlog/save" : "sys/buserwithdrawlog/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bUserWithdrawLog),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		derivedForm: function (event) {
			var url = baseURL +"sys/buserwithdrawlog/derivedForm";
			var form = document.createElement("form");
			form.style.display = 'none';
			form.action = url;
			form.method = "post";
			document.body.appendChild(form);
			form.submit();
			form.remove();
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/buserwithdrawlog/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/buserwithdrawlog/info/"+id, function(r){
                vm.bUserWithdrawLog = r.bUserWithdrawLog;
            });
		},
		/**
		 * 查看用户二维码
		 * @param e
		 */
		queryQrcode: function (e,userId) {
			let src = $(e).attr("src");
			if (!src) {
				return;
			}

			$("#userQrcodeUrl").attr("src", src);

			let index = layer.open({
				type: 1,
				title: '当前用户ID: '+userId,
				maxmin: true,
				shadeClose: true, //点击遮罩关闭层
				area : ['480px' , '500px'],
				content: $("#userQrcode").html(),
				success: function (layero, index) {
					// vm.layero = layero;
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			var processStatus=$("#processStatus").val();
            var userId=$("#userId").val();
            var userName=$("#userName").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				postData:{"userId":userId,"userName":userName,"processStatus":processStatus}
            }).trigger("reloadGrid");
		}
	}
});